package com.yfqy.admin.resource;

import com.yfqy.admin.domain.dto.QueryById;
import com.yfqy.admin.domain.dto.label.AddLabel;
import com.yfqy.admin.domain.dto.label.LabelDTO;
import com.yfqy.admin.domain.dto.label.ModifiedLabel;
import com.yfqy.admin.service.LabelService;
import com.yfqy.admin.domain.vo.common.ResultVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/internal/label")
public class LabelController {

    private final LabelService labelService;

    public LabelController(LabelService labelService) {
        this.labelService = labelService;
    }

    // 新增标签
    @PostMapping("/add")
    public ResultVO<String> addLabel(@RequestBody @Validated AddLabel label) {
        try {
            int result = labelService.addLabel(label);
            return result > 0 ? ResultVO.success("添加成功") : ResultVO.fail("添加失败");
        } catch (IllegalArgumentException e) {
            return ResultVO.fail(e.getMessage());
        }
    }

    // 删除标签
    @PostMapping("/delete")
    public ResultVO<String> deleteLabel(@RequestBody @Validated QueryById queryById) {
        try {
            int result = labelService.deleteLabel(queryById);
            return result > 0 ? ResultVO.success() : ResultVO.fail("删除失败");
        } catch (IllegalArgumentException e) {
            return ResultVO.fail(e.getMessage());
        }
    }

    // 更新标签
    @PostMapping("modify")
    public ResultVO<String> modifyLabel(@RequestBody ModifiedLabel label) {
        try {
            int result = labelService.updateLabel(label);
            return result > 0 ? ResultVO.success() : ResultVO.fail("更新失败");
        } catch (IllegalArgumentException e) {
            return ResultVO.fail(e.getMessage());
        }
    }


    // 查询标签列表
    @GetMapping("/list")
    public ResultVO<List<LabelDTO>> getLabelList(
            @RequestParam(required = false) String labelName,
            @RequestParam(required = false) Integer status) {
        List<LabelDTO> list = labelService.getLabelList(labelName, status);
        return ResultVO.data(list);
    }

}
